package MyTest;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class Test {
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			System.out.println(sdf.format(sdf.parse("2013-12-02")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for(int i=0;i<10;i++){
			try {
				Thread.sleep(2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println(String.valueOf(new Date().getTime()+"---"+new Random().nextInt(10000)));
		}
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		//c.add(Calendar.DAY_OF_MONTH,-1);
		System.out.println(randomString(20));
		System.out.println(new String("我们".getBytes(),"utf-8"));
		System.out.println(c.get(Calendar.DAY_OF_WEEK));
		System.out.println(c.get(Calendar.DATE));
		System.out.println(c.get(Calendar.MONTH));
		
		
			Calendar c3 = Calendar.getInstance();

			// 上上周星期六
			c3.add(Calendar.DATE, 0 - c3.get(Calendar.DAY_OF_WEEK) - 7 * 1);
			Date d3 = c3.getTime();
			formatToString(d3, "yyyy-MM-dd");

			// 上周星期六
			Calendar c0 = Calendar.getInstance();
			c0.add(Calendar.DATE, 0 - c0.get(Calendar.DAY_OF_WEEK));
			Date d1 = c0.getTime();
			formatToString(d1, "yyyy-MM-dd");

			// 本周星期六
			Calendar c1 = Calendar.getInstance();
			c1.add(Calendar.DATE, c1.get(Calendar.DAY_OF_WEEK));
			Date d = c1.getTime();
			formatToString(d, "yyyy-MM-dd");

			// 下周星期六
			Calendar c2 = Calendar.getInstance();
			c2.add(Calendar.DATE, 1 * 7 + c2.get(Calendar.DAY_OF_WEEK));
			Date d2 = c2.getTime();
			formatToString(d2, "yyyy-MM-dd");    
			
			 //&nbsp;&nbsp; 获取前天的现在时间
			Calendar c4 = Calendar.getInstance();
			c4.add(Calendar.DATE, -1);
			Date d4 = c4.getTime();
			formatToString(d4, "yyyy-MM-dd");
			
			//一个月的第1天的日期
		  Calendar c5 = Calendar.getInstance();   
		  c5.set(Calendar.DAY_OF_MONTH, 1);   
		  Date d5 = c5.getTime();   
		  formatToString(d5, "yyyy-MM-dd");  

	}
	
	public static void formatToString(Date date, String pattern) {
		java.text.SimpleDateFormat sf = new SimpleDateFormat(pattern);
		String str = sf.format(date);
		System.out.println(str);
	}
	
	
	private static Random randGen = null;		
	private static char[] numbersAndLetters = null;		
	public static String randomString(int length) {		         
		if (length < 1) {		             
			return null;		         
		}		         
		if (randGen == null) {		                
			randGen = new Random();		                
			numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
			"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();	
		}		        
		char [] randBuffer = new char[length];		         
		for (int i=0; i<randBuffer.length; i++) {		            
			randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];		          
			}		        
		return new String(randBuffer);		
		}
}
